//
//  AViewFormController.m
//  MClient
//
//  Created by  史正烨 on 10-4-8.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "AViewFormController.h"
#import "MServerDAO.h"
#import "MClientAppDelegate.h"
#import "MCFunction.h"
#import "MCUIToolkit.h"

@interface AViewFormController (Private)
//int actionSheetFlag;
//int endFieldIndex;
- (void) createRightBarButton;
@end


@implementation AViewFormController
@synthesize dataObjectName;
@synthesize layoutName;
@synthesize keyValue;
@synthesize initialValues;
@synthesize formData;
@synthesize formLayout;
@synthesize tabIndex;
@synthesize startIndexArray;
@synthesize viewButtons;
@synthesize subButtons;
@synthesize subDetailButtons;
@synthesize aListInfo;
@synthesize cellWidth;
@synthesize cellHeight;
@synthesize imageWidth;
@synthesize imageHeight;
@synthesize imagesCache;

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	[super viewDidLoad];
	cellWidth = self.width;
	cellHeight = 0.0;
	imageWidth = 0.0;
	imageHeight = 0.0;
	imagesCache = [[NSMutableDictionary alloc] init];
	[self leftBarButton:@"返回" withStyle:UIBarButtonItemStylePlain selector:@selector(selectCancelAction:)];
	self.tabIndex = 0;
	
	self.addHelpButton = YES;
	self.helpUrl = @"/iphone/help/chapter05-02.html";
	self.title = [formLayout objectForKey:@"lbl"];

	// 生成表单切换按钮
	if ([[formLayout objectForKey:@"slc"] count] > 0) {
		self.tableview.frame = CGRectMake(0, 0, self.width, self.height-nTabBarHeight);
		UITabBar *tabBar = [[[UITabBar alloc] initWithFrame:CGRectMake(0, self.height-nTabBarHeight, self.width, nTabBarHeight)] retain];
		tabBar.delegate = self;
		tabBar.tag = -200;
		UITabBarItem *tabBarItem = [[UITabBarItem alloc] initWithTitle:[formLayout objectForKey:@"lbl"] image:[UIImage imageNamed:@"master.png"] tag:0];
	
		NSMutableArray *tabBarItems = [[NSMutableArray alloc] initWithObjects:tabBarItem,nil];
		[tabBarItem release];
	
		int i = 1;
		startIndexArray = [[NSMutableArray alloc] init];
		for(NSMutableDictionary *subListConfig in [formLayout objectForKey:@"slc"]) {
			tabBarItem = [[UITabBarItem alloc] 
						  initWithTitle:[subListConfig objectForKey:@"lbl"]
						  image:[UIImage imageNamed:@"detail.png"]
						  tag:i];
			i++;
			[tabBarItems addObject:tabBarItem];
			[tabBarItem release];
			[startIndexArray addObject:[NSNumber numberWithInt:0]];
		}
		[tabBar setItems:tabBarItems];
		[tabBarItems release];
		[self.clientView addSubview:tabBar];
		[tabBar release];
	}
	self.aListInfo = [[NSMutableDictionary alloc] init];
}


- (void)dealloc {
	[imagesCache release];
	[viewButtons release];
    [super dealloc];	
}

#pragma mark ---- 数据加载方法 ----
- (void)getDataFromServer {	
	[imagesCache removeAllObjects];
	[[MServerDAO sharedInstance] getFormDataAsync:self.dataObjectName
									   layoutName:self.layoutName 
											  key:self.keyValue
									   isEditable:@"0"
										 delegate:self];
}

- (void)getDataFromServerSuccess: (NSMutableDictionary *)returnValue {
	
	self.formData = [returnValue retain];
	if (![self.formData objectForKey:@"data"]){ 
		[self.formData setObject:[[NSMutableDictionary alloc] init] forKey:@"data"];
	}
	
	UITabBar *tabBar = (UITabBar *)[self.clientView viewWithTag:-200];
	int i = 1;
	for(NSMutableDictionary *subListConfig in [formLayout objectForKey:@"slc"]) {
		NSString *subDataObjectName = [subListConfig objectForKey:@"dto"];
		int subCount = [[[[self.formData objectForKey:@"sdata"] 
						  objectForKey:subDataObjectName] objectForKey:@"rl"] count];
		if (subCount > 0) {
			UITabBarItem *tabBarItem = [tabBar.items objectAtIndex:i];
			tabBarItem.badgeValue = [NSString stringWithFormat:@"%d",subCount];
		}
		i++;
	}
	[self createRightBarButton];
	[self reloadData];
}

#pragma mark ---- UITableView方法 ----
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	if (tabIndex == 0) {
		// 主表单
		return [[formLayout objectForKey:@"stn"] count];
	}
	// 子表单
	return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if ([[[formLayout objectForKey:@"stn"] objectAtIndex:section] objectForKey:@"lbl"] == nil ||
		[@"" isEqualToString:[[[formLayout objectForKey:@"stn"] objectAtIndex:section] objectForKey:@"lbl"]]) {
		return 0;
	}	
	return 30;
}

//每一行的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
	if (tabIndex != 0) {
		int row = [indexPath row];
		NSString *subDataObjectName = [[[formLayout objectForKey:@"slc"]
										objectAtIndex:tabIndex-1]
									   objectForKey:@"dto"];
		
		NSMutableArray *cellInfos = [MCUIToolkit getAListInfo:[[formLayout objectForKey:@"slc"] objectAtIndex:tabIndex-1]
													 listData:[[[self.formData objectForKey:@"sdata"] objectForKey:subDataObjectName]objectForKey:@"rl"]
													 rowIndex:row
													cellWidth:cellWidth];
		[self.aListInfo setObject:cellInfos forKey:[NSNumber numberWithInt:row]];
		if ([cellInfos count] <= 0) {
			return 0.0;
		}
		cellHeight = [[[cellInfos objectAtIndex:([cellInfos count] - 1)] objectForKey:@"cellHeight"] floatValue];
		if (cellHeight < imageHeight) {
			cellHeight = imageHeight + 12.0;
			return cellHeight;
		}
		if (cellHeight == 0.0) {
			return cellHeight;
		}
		return cellHeight + 12.0;	
	}
	return 44;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger) section {
	UILabel *sectionHead = [[UILabel alloc] init];
	if (self.tabIndex == 0) {
		sectionHead.textAlignment = UITextAlignmentCenter;
		sectionHead.text = [[[formLayout objectForKey:@"stn"] objectAtIndex:section] objectForKey:@"lbl"];
		sectionHead.font = [UIFont boldSystemFontOfSize:20];
		sectionHead.textColor = [UIColor whiteColor];
		sectionHead.backgroundColor = [UIColor colorWithPatternImage:self.userDefaults.skin_bgSectionImg];
		return sectionHead;
	}	
	sectionHead.textAlignment = UITextAlignmentCenter;
	sectionHead.text = [[[formLayout objectForKey:@"slc"] objectAtIndex:tabIndex-1] objectForKey:@"lbl"];
	sectionHead.font = [UIFont boldSystemFontOfSize:20];
	sectionHead.textColor = [UIColor whiteColor];
	sectionHead.backgroundColor = [UIColor colorWithPatternImage:self.userDefaults.skin_bgSectionImg];
	return sectionHead;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	if (tabIndex == 0) {
		return [[[[formLayout objectForKey:@"stn"] objectAtIndex:section] objectForKey:@"item"] count];
	}
	NSString *subDataObjectName = [[[formLayout objectForKey:@"slc"]
									objectAtIndex:tabIndex-1]
								   objectForKey:@"dto"];
	return [[[[self.formData objectForKey:@"sdata"] objectForKey:subDataObjectName] objectForKey:@"rl"] count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
	if (tabIndex == 0) {
		return [[[formLayout objectForKey:@"stn"] objectAtIndex:section] objectForKey:@"lbl"] ;
	}
	return nil;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	int section = [indexPath section];
	int row = [indexPath row];
	if (tabIndex == 0) {
		// 主表单
		NSMutableDictionary *appItem = [[[[formLayout objectForKey:@"stn"] 
										  objectAtIndex:section]
										 objectForKey:@"item"]
										objectAtIndex:row];
		UITableViewCell *cell = [MCFunction createCellForViewForm:tableView appItem:appItem indexPath:indexPath formView:self.clientView];
		
		UILabel *labelField = (UILabel *)[cell.contentView viewWithTag:-1];
		labelField.text = [MCUIToolkit getFieldLabel:appItem];
		UILabel *valueField = (UILabel *)[cell.contentView viewWithTag:-2];
		NSString *valueText = [MCUIToolkit getFieldTextValue:self.formData
													 appItem:appItem
												selectOption:[self.formData objectForKey:@"so"]];
		NSString *fieldType = [MCUIToolkit getFieldType:appItem];
		char cFieldType;
		strncpy(&cFieldType, [fieldType UTF8String], 1);
		if (cFieldType == 'H'||cFieldType == 'I'||cFieldType == 'K'||cFieldType == 'L') {
			NSArray *selectIcons = [valueText componentsSeparatedByString:@"::"];
			if ([selectIcons count] > 1) {
				NSString *iconLabel = [selectIcons objectAtIndex:1];
				NSArray *icons = [iconLabel componentsSeparatedByString:@","];
				for (int j = 0; j < [icons count]; j++) {
					NSString *icon = [icons objectAtIndex:j];
					UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(32 * j, 0, 24, 24)];
					imageView.image = [MCFunction scaleAndRotateImage:[UIImage imageNamed:icon] width:24.0 height:24.0];
					imageView.backgroundColor = [UIColor clearColor];
					[valueField addSubview:imageView];
					[imageView release];
				}
				return cell;
			} 
		}
		valueField.text = valueText;
		return cell;	
	}
	else
	{
		// 子表单
		int row = [indexPath row];
		UITableViewCell *cell = [MCUIToolkit getAListCell:[self.aListInfo objectForKey:[NSNumber numberWithInt:row]]
											  imagesCache:imagesCache
												 rowIndex:row
											isMultiSelect:NO
												cellWidth:cellWidth
											   cellHeight:cellHeight
											   imageWidth:imageWidth
											  imageHeight:imageHeight];
		return cell;
	}
}	

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	int row = [indexPath row];
	if ([self tabIndex] == 0) {
		UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
		if (cell.accessoryType != UITableViewCellAccessoryDisclosureIndicator) {
			[tableView deselectRowAtIndexPath:indexPath animated:YES];
			return;
		}
		int section = [indexPath section];
		int row = [indexPath row];
		NSMutableDictionary *appItem = [[[[formLayout objectForKey:@"stn"] 
													objectAtIndex:section]
													objectForKey:@"item"]
													objectAtIndex:row];
		
	
		NSString *name = [MCUIToolkit getFieldName:appItem];
				
		NSMutableDictionary *datas = [self.formData objectForKey:@"data"];
		
		NSString *fieldData = [datas objectForKey:name];
		if (fieldData == nil) {
			NSMutableDictionary *fieldData = [NSMutableDictionary dictionary];
			NSString *type = [MCUIToolkit getFieldType:appItem];	
			if ([[datas objectForKey:name] count] == 0) {
				if ([type isEqualToString:@"J"] || 
					[type isEqualToString:@"S"] || 
					[type isEqualToString:@"Y"]) {
					NSMutableArray *fi = [NSMutableArray array];
					[fieldData setObject:fi forKey:@"fi"];
					[fieldData setObject:@"" forKey:@"df"];
					[[self.formData objectForKey:@"data"] setObject:fieldData forKey:name];
				}
				else {
					[fieldData setObject:@"" forKey:@"value"];
					[[self.formData objectForKey:@"data"] setObject:fieldData forKey:name];
				}
			}
		}
		[tableView deselectRowAtIndexPath:indexPath animated:YES];
		[MCFunction selectDetailForViewForm:self
								   withCell:cell
							  withIndexPath:indexPath
									appItem:appItem 
									   data:[datas objectForKey:name]];
		
		return;
	}
	else {
		actionSheetFlag = row;
		NSString *subDataObjectName = [[[formLayout objectForKey:@"slc"] 
										objectAtIndex:tabIndex-1]
									   objectForKey:@"dto"];
		
		NSMutableDictionary *record = [[[[self.formData objectForKey:@"sdata"] objectForKey:subDataObjectName] objectForKey:@"rl"]objectAtIndex:row];
		NSMutableDictionary *subListConfig = [[self.formLayout objectForKey:@"slc"] objectAtIndex:tabIndex - 1];
		
		NSMutableArray *dataButtons = [record objectForKey:@"btn"];
		NSMutableArray *cfgButtons =[subListConfig objectForKey: @"dbtn"];
		
		self.subDetailButtons = [MCUIToolkit getButtons:dataButtons cfgButtons:cfgButtons];	
		if ([subDetailButtons count] <= 0) {
			return;
		}		
		if ([subDetailButtons count] == 1) {
			NSString *pKey = @"";
			if ([record objectForKey:@"key"] != nil) {
				pKey = [record objectForKey:@"key"];
			}
			NSMutableDictionary *button = [subDetailButtons objectAtIndex:0];
			if ([MCErrorManagement showAuthMessage:button]) {
				return;
			}
			NSString *buttonId = [button objectForKey:@"id"] == nil?@"":[button objectForKey:@"id"];
			NSMutableDictionary *parameters = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
												buttonId,@"buttonId",
												@"list_rec",@"buttonType",
												subDataObjectName,@"dataObjectName",
												record,@"formData",
												pKey, @"primaryKey", nil];
				
			if ([button objectForKey:@"cfmi"] != nil && ![[button objectForKey:@"cfmi"] isEqualToString:@""]){
				[MCFunction pushViewByUrl:[button objectForKey:@"url"] 
							   parameters:parameters 
						   confirmMessage:[button objectForKey:@"cfmi"] naviController:self.navigationController];
			}else{
				[MCFunction pushViewByUrl:[button objectForKey:@"url"] 
							   parameters:parameters
						   confirmMessage:nil naviController:self.navigationController];
					
			}
			[parameters release];
		}
		else if ([subDetailButtons count] > 1)
		{
			UIActionSheet *menu = [MCUIToolkit actionSheet:subDetailButtons
												   withTag:0
											  withDelegate:self
												 withTitle:@"请选择明细操作"];
			[menu showInView:self.clientView.window];
		}
		[tableView deselectRowAtIndexPath:indexPath animated:YES];
		return;
	}
}

#pragma mark ---- UITabBar事件处理方法 ----
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
	self.tabIndex = item.tag;
	[self.clientView bringSubviewToFront:tabBar];
	[self.tableview reloadData];
	[self createRightBarButton];
}


- (void) createRightBarButton {
	if (tabIndex == 0) {
		// 生成主表单的操作入口按钮
		NSMutableArray *cfgButtons = [formLayout objectForKey:@"btn"];
		NSMutableArray *dataButtons = [formData objectForKey:@"btn"];
		self.viewButtons = [MCUIToolkit getButtons:dataButtons cfgButtons:cfgButtons];
		
		[self rightBarButton:@"操作" withStyle:UIBarButtonItemStyleDone selector:@selector(selectAction:)];
		if ([viewButtons count] == 1) {
			self.rightBarButtonItemTitle = [[viewButtons objectAtIndex:0] objectForKey:@"lbl"] ;
		}
		else if ([viewButtons count] > 1) {
			self.rightBarButtonItemTitle = @"操作";
		}else {
			self.rightBarButtonItem = nil;
		}
	}
	else {
		NSString *subDataObjectName = [[[formLayout objectForKey:@"slc"] 
										objectAtIndex:tabIndex-1]
									   objectForKey:@"dto"];
		
		NSMutableArray *cfgButtons = [[[formLayout objectForKey:@"slc"] objectAtIndex:tabIndex-1] objectForKey:@"btn"];
		NSMutableArray *dataButtons = [[[formData objectForKey:@"sdata"] objectForKey:subDataObjectName] objectForKey: @"btn"];
		
		self.subButtons = [MCUIToolkit getButtons:dataButtons cfgButtons:cfgButtons];
		if ([subButtons count] == 1) {
			self.rightBarButtonItemTitle = [[subButtons objectAtIndex:0] objectForKey:@"lbl"];
		}
		else if ([subButtons count] > 1) {
			self.rightBarButtonItemTitle = @"操作...";
		}else {
			self.rightBarButtonItem = nil;
		}
		NSMutableArray *listFields = [[[formLayout objectForKey:@"slc"] objectAtIndex:tabIndex-1] objectForKey:@"item"];
		if ([[[[self.formData objectForKey:@"sdata"] objectForKey:subDataObjectName] objectForKey:@"rl"] count] > 0) {
			NSMutableDictionary *record = [[[[self.formData objectForKey:@"sdata"] objectForKey:subDataObjectName] objectForKey:@"rl"] objectAtIndex:0];
			for (int i = 0; i < [listFields count]; i++) {
				NSMutableDictionary *appItem = [listFields objectAtIndex:i];
				if ([[appItem objectForKey:@"v"] boolValue]) {
					NSString *fieldType = [MCUIToolkit getFieldType:appItem];
					NSString *fieldName = [MCUIToolkit getFieldName:appItem];
					NSMutableDictionary *fieldData = [[record objectForKey:@"data"] objectForKey:fieldName];
					if ([fieldType isEqualToString:@"S"]) {
						imageWidth = nImageWidthOfCell;
						imageHeight = nImageHeightOfCell;
						NSMutableDictionary *style = [fieldData objectForKey:@"style"];
						if (nil != style) {
							if ([style objectForKey:@"width"]) {
								imageWidth = [[style objectForKey:@"width"] floatValue];
							}
							if ([style objectForKey:@"height"]) {
								imageHeight = [[style objectForKey:@"height"] floatValue];
							}
						}
						if (imageWidth > self.width) {
							imageHeight = self.width * imageHeight / imageWidth;
							imageWidth = self.width;
						}
						cellWidth = self.width - imageWidth;
					}
				}
			}
		}
	}
}

- (void) selectAction: (id) sender {
	UIActionSheet *menu;
	
	if ([self tabIndex] == 0) {
		if ([viewButtons count] == 1) {
			NSMutableDictionary *button = [viewButtons objectAtIndex:0];
			if ([MCErrorManagement showAuthMessage:button]) {
				return;
			}
			NSString *buttonId = [button objectForKey:@"id"] == nil?@"":[button objectForKey:@"id"];
			NSMutableDictionary *parameters = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
											   buttonId,@"buttonId",
											   @"view_form",@"buttonType",
											   self.dataObjectName,@"dataObjectName",
											   self.formData,@"formData", nil];
			
			if ([button objectForKey:@"cfmi"] != nil && ![[button objectForKey:@"cfmi"] isEqualToString:@""]){
				[MCFunction pushViewByUrl:[button objectForKey:@"url"] 
							   parameters:parameters 
						   confirmMessage:[button objectForKey:@"cfmi"] naviController:self.navigationController];
			}else{
				[MCFunction pushViewByUrl:[button objectForKey:@"url"] 
							   parameters:parameters 
						   confirmMessage:nil naviController:self.navigationController];
			}
			[parameters release];
			return;
		}
		
		//导航bar上面的操作按钮弹出菜单
		menu = [MCUIToolkit actionSheet:viewButtons
								withTag:0
						   withDelegate:self
							  withTitle:@"选择操作"];
		[menu showFromTabBar:(UITabBar *)[self.navigationController view]];
	}
	else {
		NSString *subDataObjectName = [[[formLayout objectForKey:@"slc"] 
										objectAtIndex:tabIndex - 1]
									   objectForKey:@"dto"];
		
		if ([subButtons count] == 1) {
			// 响应明细表单的表单按钮
			NSMutableDictionary *button = [subButtons objectAtIndex:0];
			if ([MCErrorManagement showAuthMessage:button]) {
				return;
			}
			NSString *buttonId = [button objectForKey:@"id"] == nil?@"":[button objectForKey:@"id"];
			NSMutableDictionary *record = [[formData objectForKey:@"sdata"] objectForKey:subDataObjectName];
			
			NSMutableDictionary *url = [button objectForKey:@"url"];
			NSMutableDictionary *parameters = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
											   buttonId,@"buttonId",
											   @"list_form",@"buttonType",
											   subDataObjectName,@"dataObjectName",
											   record,@"formData",nil];
			[MCFunction pushViewByUrl:url 
						   parameters:parameters
					   confirmMessage:[[button objectForKey:@"comfirm"] boolValue]?[button objectForKey:@"comfirmString"]:nil 
					   naviController:self.navigationController];
			[parameters release];
			return;			
		}
		
		// 弹出明细表单的表单按钮
		menu = [MCUIToolkit actionSheet:subButtons
								withTag:0
						   withDelegate:self
							  withTitle:@"选择操作"];
		actionSheetFlag = -1;
		[menu showFromTabBar:(UITabBar *)[self.navigationController view]];
	}
}

- (void) selectCancelAction: (id) sender {
	[self.navigationController popViewControllerAnimated:YES];
}

- (void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
	if (tabIndex == 0) {
		// 响应主表单的表单按钮
		if (buttonIndex == [viewButtons count]) {
			// 按下取消按钮
			return;
		}
		NSMutableDictionary *button = [viewButtons objectAtIndex:buttonIndex];
		if ([MCErrorManagement showAuthMessage:button]) {
			return;
		}
		NSString *buttonId = [button objectForKey:@"id"] == nil?@"":[button objectForKey:@"id"];
		NSMutableDictionary *parameters = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
										   buttonId,@"buttonId",
										   @"view_form",@"buttonType",
										   self.dataObjectName,@"dataObjectName",
										   self.formData,@"formData", nil];
		
		if ([button objectForKey:@"cfmi"] != nil && ![[button objectForKey:@"cfmi"] isEqualToString:@""]){
			[MCFunction pushViewByUrl:[button objectForKey:@"url"] 
						   parameters:parameters 
					   confirmMessage:[button objectForKey:@"cfmi"] 
					   naviController:self.navigationController];
		}else{
			[MCFunction pushViewByUrl:[button objectForKey:@"url"]
						   parameters:parameters 
					   confirmMessage:nil 
					   naviController:self.navigationController];
		}
		[parameters release];		
	}
	else {
		if (actionSheetFlag == -1) {
			// 响应明细表单的表单按钮
			if (buttonIndex == [subButtons count]) {
				// 按下取消按钮
				return;
			}
			NSString *subDataObjectName = [[[formLayout objectForKey:@"slc"] 
											objectAtIndex:tabIndex-1]
										   objectForKey:@"dto"];
			
			NSMutableDictionary *record = [[formData objectForKey:@"sdata"] objectForKey:subDataObjectName];
			NSMutableDictionary *listLayoutButton = [subButtons objectAtIndex:buttonIndex];
			
			NSMutableDictionary *url = [listLayoutButton objectForKey:@"url"];
			NSString *buttonId = [listLayoutButton objectForKey:@"id"] == nil?@"":[listLayoutButton objectForKey:@"id"];
			NSMutableDictionary *parameters = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
											   buttonId,@"buttonId",
											   @"list_form",@"buttonType",
											   subDataObjectName,@"dataObjectName",
											   record,@"formData",nil];
			[MCFunction pushViewByUrl:url 
						   parameters:parameters
					   confirmMessage:[[listLayoutButton objectForKey:@"comfirm"] boolValue]?[listLayoutButton objectForKey:@"comfirmString"]:nil 
					   naviController:self.navigationController];
			[parameters release];
		}		
		else {
			// 响应明细表单的明细按钮
			if (buttonIndex == [subDetailButtons count]) {
				// 按下取消按钮
				return;
			}
			NSString *subDataObjectName = [[[formLayout objectForKey:@"slc"] 
											objectAtIndex:tabIndex-1]
										   objectForKey:@"dto"];
			
			NSMutableDictionary *record = [[[[self.formData objectForKey:@"sdata"] objectForKey:subDataObjectName]objectForKey:@"rl"]objectAtIndex:actionSheetFlag];
			NSMutableDictionary *button = [subDetailButtons objectAtIndex:buttonIndex];
			if ([MCErrorManagement showAuthMessage:button]) {
				return;
			}
			NSString *pKey = @"";
			if ([record objectForKey:@"key"] != nil) {
				pKey = [record objectForKey:@"key"];
			}
			NSString *buttonId = [button objectForKey:@"id"] == nil?@"":[button objectForKey:@"id"];
			NSMutableDictionary *url = [button objectForKey:@"url"];
			NSMutableDictionary *parameters = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
											   buttonId,@"buttonId",
											   @"list_rec",@"buttonType",
											   subDataObjectName,@"dataObjectName",
											   record,@"formData",nil];
			
			if ([[button objectForKey:@"comfirm"] boolValue])
				[MCFunction pushViewByUrl:url 
							   parameters:parameters 
						   confirmMessage:[button objectForKey:@"comfirmString"] 
						   naviController:self.navigationController];
			else
				[MCFunction pushViewByUrl:url
							   parameters:parameters 
						   confirmMessage:nil 
						   naviController:self.navigationController];
			[parameters release];
		}
	}
}


@end
